<template>
  <el-dialog
    :title="!dataForm.id ? '新增禅道BUG' : !disabled ? '修改禅道BUG' : '查看禅道BUG'"
    :close-on-click-modal="false" width="60%" top="2vh"
    :append-to-body="true"
    v-if="zentaoVisible"
    :visible.sync="zentaoVisible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="Bug标题" prop="title">
            <el-input v-model="dataForm.title" :disabled="disabled" :maxlength="200" placeholder="Bug标题"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="所属项目" prop="project">
            <el-select v-model="dataForm.project"
                       :disabled="disabled"
                       class="width100"
                       filterable
                       placeholder="所属项目"
                       size="small"
                       @change="projectChange(dataForm.project)">
              <el-option
                v-for="item in projectList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属执行" prop="execution">
            <el-select v-model="dataForm.execution"
                       :disabled="disabled"
                       class="width100"
                       filterable
                       placeholder="所属执行"
                       size="small"
                       @change="executionChange(dataForm.execution)">
              <el-option
                v-for="item in executionList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="所属模块" prop="module">
            <select-tree v-model="dataForm.module"
                         :disabled="disabled"
                         :nodeKey="moduleTree.nodeKey"
                         :treeData="moduleTree.treeDataOptions"
                         :treeProps="moduleTree.treeProps"
                         allowTrunk
                         clearable
                         placeholder="请输入所属模块"
                         show-search
                         @change="moduleChange">
            </select-tree>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="严重程度" prop="severity">
            <el-select v-model="dataForm.severity"
                       :disabled="disabled"
                       class="width100"
                       filterable
                       placeholder="严重程度"
                       size="small">
              <el-option key="1" label="轻微" value="1"></el-option>
              <el-option key="2" label="一般" value="2"></el-option>
              <el-option key="3" label="严重" value="3"></el-option>
              <el-option key="4" label="阻断" value="4"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="优先级" prop="pri">
            <el-select v-model="dataForm.pri"
                       :disabled="disabled"
                       class="width100"
                       filterable
                       placeholder="优先级"
                       size="small">
              <el-option key="1" label="低" value="1"></el-option>
              <el-option key="2" label="中" value="2"></el-option>
              <el-option key="3" label="高" value="3"></el-option>
              <el-option key="4" label="最高" value="4"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="影响版本" prop="openedbuild">
            <el-select v-model="dataForm.openedbuild"
                       :disabled="disabled"
                       class="width100"
                       filterable
                       placeholder="影响版本"
                       size="small">
              <el-option
                v-for="item in openedbuildList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="创建人" prop="openedby">
            <el-select v-model="dataForm.openedby"
                       :disabled="disabled"
                       class="width100"
                       filterable
                       placeholder="创建人"
                       size="small">
              <el-option
                v-for="item in assignedtoList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="指派给" prop="assignedto">
            <el-select v-model="dataForm.assignedto"
                       :disabled="disabled"
                       class="width100"
                       filterable
                       placeholder="指派给"
                       size="small">
              <el-option
                v-for="item in assignedtoList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="截止日期" prop="deadline">
            <el-date-picker
              v-model="dataForm.deadline"
              :disabled="disabled"
              type="date"
              placeholder="截止日期"
              value-format="yyyy-MM-dd HH:mm:ss"
              default-time="00:00:00">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="Bug类型" prop="type">
            <el-dict code="WTLXYF_LIST" v-model="dataForm.type" :disabled="disabled" placeholder="Bug类型" clearable
                     filterable></el-dict>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="关键词" prop="keywords">
            <el-input v-model="dataForm.keywords" :disabled="disabled" :maxlength="100" placeholder="关键词"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="重现步骤" prop="steps">
            <ueditor v-model="dataForm.steps" custom-id="zentaoEditer" :height="200" :disabled="disabled"></ueditor>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="zentaoVisible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SelectTree from '@/components/select-tree'

export default {
  data() {
    return {
      disabled: false,
      zentaoVisible: false,
      projectList: [],
      executionList: [],
      priList: [],
      openedbuildList: [],
      assignedtoList: [],
      ueditorId: 'zentaoEditer',
      dataForm: {
        id: '',
        project: '',
        product: '',
        injection: '',
        identify: '',
        branch: '',
        module: '',
        execution: '',
        plan: '',
        story: '',
        storyversion: '',
        task: '',
        totask: '',
        tostory: '',
        title: '',
        keywords: '',
        severity: '',
        pri: '',
        type: '',
        os: '',
        browser: '',
        hardware: '',
        found: '',
        steps: '',
        status: '',
        substatus: '',
        color: '',
        confirmed: '',
        activatedcount: '',
        activateddate: '',
        feedbackby: '',
        notifyemail: '',
        mailto: '',
        openedby: '',
        openeddate: '',
        openedbuild: '',
        assignedto: '',
        assigneddate: '',
        deadline: '',
        resolvedby: '',
        resolution: '',
        resolvedbuild: '',
        resolveddate: '',
        closedby: '',
        closeddate: '',
        duplicatebug: '',
        linkbug: '',
        case: '',
        caseversion: '',
        feedback: '',
        result: '',
        repo: '',
        mr: '',
        entry: '',
        lines: '',
        v1: '',
        v2: '',
        repotype: '',
        issuekey: '',
        testtask: '',
        lasteditedby: '',
        lastediteddate: '',
        deleted: '',
        wtdh: ''
      },
      moduleTree: {
        treeDataOptions: [],
        // 树节点配置选项
        treeProps: {
          parent: 'parent',
          value: 'value',
          label: 'label',
          children: 'children'
        },
        nodeKey: 'value'
      },
      dataRule: {
        project: [
          {required: true, message: '所属项目不能为空', trigger: 'blur'}
        ],
        execution: [
          {required: true, message: '所属执行不能为空', trigger: 'blur'}
        ],
        module: [
          {required: true, message: '所属模块不能为空', trigger: 'blur'}
        ],
        severity: [
          {required: true, message: '严重程度不能为空', trigger: 'blur'}
        ],
        pri: [
          {required: true, message: '优先级不能为空', trigger: 'blur'}
        ],
        openedbuild: [
          {required: true, message: '影响版本不能为空', trigger: 'blur'}
        ],
        assignedto: [
          {required: true, message: '指派给不能为空', trigger: 'blur'}
        ],
        type: [
          {required: true, message: 'Bug类型不能为空', trigger: 'blur'}
        ],
        steps: [
          {required: true, message: '重现步骤不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  components: {
    SelectTree
  },
  methods: {
    initDyWtdh(wtdh) {
      // this.disabled = disabled
      // this.dataForm.id = id || ''
      this.zentaoVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.$http({
          url: `/zr/dytz/info/${wtdh}`,
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.title = data.dytz.wtbt
            this.dataForm.keywords = data.dytz.wtdh
            this.dataForm.steps = data.dytz.wtms
            this.dataForm.wtdh = wtdh

            // 严重程度
            this.dataForm.severity = '2'
            this.dataForm.pri = '2' // 优先级
            this.dataForm.type = 'codeerror'
          }
        })
        this.$http({
          url: `/zt/bug/createInitData`,
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.projectList = data.projectList
            if (this.projectList && this.projectList.length > 0) {
              this.dataForm.project = this.projectList[0].value
              this.projectChange(this.projectList[0].value)
            }
          }
        })
        // if (this.dataForm.id) {
        //   this.$http({
        //     url: `/zt/bug/info/${this.dataForm.id}`,
        //     method: 'get'
        //   }).then(({data}) => {
        //     if (data && data.code === 0) {
        //       this.dataForm = data.bug
        //     }
        //   })
        // }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm']
        .validate((valid) => {
          if (valid) {
            this.$http({
              url: `/zt/bug/converZentao`,
              method: 'post',
              data: this.dataForm
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.zentaoVisible = false
              }
            })
          }
        })
    },
    projectChange(projectId) {
      this.$http({
        method: 'get',
        params: {'projectId': projectId},
        url: '/zt/bug/projectChange'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.executionList = data.executionList
          if (this.executionList && this.executionList.length > 0) {
            this.dataForm.execution = this.executionList[0].value
          }
          this.moduleTree.treeDataOptions = this.treeDataTranslate(data.moduleList, 'value', 'parent', 'children')
          if (!this.moduleTree.treeDataOptions || this.moduleTree.treeDataOptions.length === 0) {
            this.dataForm.module = ''
          }
        }
      })
    },
    executionChange(executionId) {
      this.$http({
        method: 'get',
        params: {'executionId': executionId},
        url: '/zt/bug/executionChange'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.assignedtoList = data.assignedtoList
          this.openedbuildList = data.openedbuildList
          // 创建人设置默认当前人
          let obj = this.assignedtoList.find((item) => {
            return item.label === this.$store.state.user.realName
          })
          this.dataForm.openedby = obj ? obj.value : undefined
        }
      })
    },
    moduleChange(val) {
      this.dataForm.module = val
    }
  }
}
</script>
